import 'package:dulu/config/global.dart';
import 'package:dulu/iconfont.dart';
import 'package:dulu/manager/main_manager.dart';
import 'package:dulu/pages/statistics.dart';
import 'package:dulu/pages/table.dart';
import 'package:dulu/route-trans.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';
import 'package:provider/provider.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int year = 2021;
  int month = 1;
  int day = 1;
  int mood = 1;

  getMoodState(mood) {
    Map state = {
      'color': Global.happyColor,
      'btntext': 'Mark今天的好心情！',
    };
    switch (mood) {
      case 1:
        state = {
          'color': Global.happyColor,
          'btntext': 'Mark今天的好心情！',
        };
        break;
      case 0:
        state = {
          'color': Global.generalColor,
          'btntext': '平平淡淡才是真嘛~',
        };
        break;
      case -1:
        state = {
          'color': Global.sadColor,
          'btntext': '我的快乐消失了...',
        };
        break;
      default:
    }

    return state;
  }

  @override
  void initState() {
    year = DateTime.now().year;
    month = DateTime.now().month;
    day = DateTime.now().day;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    MainManager _mainManager = Provider.of<MainManager>(context);
    return Scaffold(
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(
          color: Color(0xFFFFFFFF),
          image: DecorationImage(
            image: AssetImage("assets/images/bg.png"),
            repeat: ImageRepeat.repeat,
            // fit: BoxFit.fitWidth,
          ),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(height: Global.padding.top),
            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  IconButton(
                    icon: Icon(IconFont.chart, color: Global.fontColor),
                    onPressed: () {
                      Navigator.of(context).push(CustomRoute(StatisticsPage(
                        year: year,
                        month: month,
                        day: day,
                      )));
                    },
                  ),
                ],
              ),
            ),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  InkWell(
                    onTap: () {
                      DatePicker.showDatePicker(
                        context,
                        pickerTheme: DateTimePickerTheme(confirm: Text('确定')),
                        onMonthChangeStartWithFirstDate: true,
                        minDateTime: DateTime.parse('2021-01-01'),
                        maxDateTime: DateTime.now(),
                        initialDateTime: DateTime.now(),
                        dateFormat: 'yyyy-MMMM-dd',
                        locale: DateTimePickerLocale.zh_cn,
                        onConfirm: (dateTime, List<int> index) {
                          // widget.onDateChange(dateTime);
                          setState(() {
                            year = dateTime.year;
                            month = dateTime.month;
                            day = dateTime.day;
                          });
                        },
                      );
                    },
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Text(
                          '$year  $month月 $day日',
                          style: TextStyle(
                            color: Global.fontColor,
                            fontSize: 20,
                            fontFamily: 'Arciform',
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        Icon(
                          Icons.arrow_forward_ios,
                          color: Global.fontColor,
                          size: 14,
                        ),
                      ],
                    ),
                  ),
                  SizedBox(height: 10),
                  Text(
                    '今天又是崭新的一天！',
                    style: TextStyle(
                      color: Global.fontColor.withOpacity(.5),
                      fontSize: 13.5,
                      fontFamily: 'Kuaile',
                      fontWeight: FontWeight.w800,
                    ),
                  ),
                  SizedBox(height: 20),
                  Container(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        InkWell(
                          onTap: () {
                            setState(() => {mood = 1});
                          },
                          child: Container(
                            width: 90,
                            child: Opacity(
                              opacity: mood == 1 ? 1 : 0.4,
                              child: Image.asset('assets/images/happy.png'),
                            ),
                          ),
                        ),
                        SizedBox(width: 10),
                        InkWell(
                          onTap: () {
                            setState(() => {mood = 0});
                          },
                          child: Container(
                            width: 90,
                            child: Opacity(
                              opacity: mood == 0 ? 1 : 0.4,
                              child: Image.asset('assets/images/general.png'),
                            ),
                          ),
                        ),
                        SizedBox(width: 10),
                        InkWell(
                          onTap: () {
                            setState(() => {mood = -1});
                          },
                          child: Container(
                            width: 90,
                            child: Opacity(
                              opacity: mood == -1 ? 1 : 0.4,
                              child: Image.asset('assets/images/sad.png'),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(height: 40),
                  InkWell(
                    onTap: () {
                      var queryRes =
                          _mainManager.queryMoodFromDate(year, month, day);
                      if (queryRes == null) {
                        int id = DateTime.now().millisecondsSinceEpoch;
                        _mainManager.insert(id, year, month, day, mood, []);
                      } else {
                        _mainManager.updateMood(queryRes['id'], mood);
                      }
                      Navigator.of(context).push(CustomRoute(StatisticsPage(
                        year: year,
                        month: month,
                        day: day,
                      )));
                    },
                    child: Container(
                      width: 200,
                      padding: EdgeInsets.fromLTRB(10, 7, 10, 7),
                      decoration: BoxDecoration(
                        color: getMoodState(mood)['color'],
                        borderRadius: BorderRadius.circular(20),
                        boxShadow: [
                          BoxShadow(
                            color: getMoodState(mood)['color'].withOpacity(.5),
                            offset: Offset(2, 3),
                            blurRadius: 3,
                            spreadRadius: 1,
                          )
                        ],
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Icon(
                            IconFont.manual,
                            color: Global.backgroundColor,
                            size: 20,
                          ),
                          SizedBox(width: 7),
                          Text(
                            getMoodState(mood)['btntext'],
                            style: TextStyle(
                              fontWeight: FontWeight.w900,
                              fontSize: 16,
                              color: Global.backgroundColor,
                              fontFamily: 'Kuaile',
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  SizedBox(height: 80),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
